package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.CampoPlanoDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.*;
/**
 * 
 * @author jsanchez
 */
public class CampoPlanoDao extends DAO {

    public CampoPlanoDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, ORDEN, IDREGISTRO, FORMULAVALIDACION, TIPO,FORMULAVALOR,VALIDACIONADICIONAL FROM CAMPO_PLANO WHERE IDREGISTRO = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            Collection c = new ArrayList();
            for (; resul.next(); c.add(getDatos(resul, new CampoPlanoDTO(1)))) {

            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, ORDEN, IDREGISTRO, FORMULAVALIDACION, TIPO,FORMULAVALOR,VALIDACIONADICIONAL FROM CAMPO_PLANO");
            for (resul = prep.executeQuery(); resul.next(); c.add(getDatos(resul, new CampoPlanoDTO(1)))) {
                ;
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        CampoPlanoDTO dto = (CampoPlanoDTO) obj;
        try {
            /*connectionIgFormato = */            getConnection();
            if (dto.getId() == null || dto.getId().intValue() < 0) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO CAMPO_PLANO(NOMBRE, ORDEN, IDREGISTRO, FORMULAVALIDACION, TIPO,FORMULAVALOR,VALIDACIONADICIONAL, ID) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("CAMPO_PLANO"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE CAMPO_PLANO SET NOMBRE=?, ORDEN=?, IDREGISTRO=?, FORMULAVALIDACION=?,FORMULAVALOR=?, TIPO=?, VALIDACIONADICIONAL=? WHERE ID=?");
            }
            setDatos(prep, dto, "NOMBRE, ORDEN, IDREGISTRO, FORMULAVALIDACION, TIPO,FORMULAVALOR,VALIDACIONADICIONAL, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM CAMPO_PLANO WHERE IDREGISTRO = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM CAMPO_PLANO WHERE NOMBRE = ? AND IDREGISTRO = ?");
            prep.setString(1, ((CampoPlanoDTO) obj).getNombre().trim());
            prep.setInt(2, ((CampoPlanoDTO) obj).getIdRegistro().intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
